<?php
namespace Home\Controller;
use Think\Controller;

// 开发者：杨晓杰，
// 邮箱：2902468225@qq.com 


// 学生作品控制器
class StdproductController extends Controller {
	private $dbName='std_product';
	// 学生作品列表
	public function listPage(){
		tag('func2');
		$where['std_id']=$_ENV['user']['id'];
		$all=M($this->dbName)->where($where)->order('create_time desc')->select();
		$data=R('Myfunc/Myfunc/pageDeal',array('data'=>$all));
		$this->assign('data',$data);
		// dump($data);
		R('Page/productionList');
	}
	// 学生作品详情
	public function detail(){
		tag('func2');
		$where['std_id']=$_ENV['user']['id'];
		$where['product_id']=I('get.id');
		$data['production']=M($this->dbName)->where($where)->find();
		$this->assign('data',$data);
		// dump($data);
		R('Page/productionDetail');
	}
	// 添加作品页面
	public function addPage(){
		tag('func2');
		R('Page/productionAdd');
	}
	// 添加作品接口
	public function add(){
		tag('func2');
		$data=I('post.');

		if($data['file']){
			$data['product_file']=$data['file'];
			$data['product_type']=2;
		}
		// 图片上传
		$img=R('Myfunc/Myfunc/upload');
		if($img){
			$data['product_file']=$img;
			$temp=explode('.',$data['product_file']);
			if($temp[1]=='jpeg'||$temp[1]=='jpg'||$temp[1]=='png'||$temp[1]=='gif')$data['product_type']=0;
			else $data['product_type']=1;
		}

		if(!$data)return;
		$data['std_id']=$_ENV['user']['id'];
		$data['update_time']=$data['create_time']=date('Y-m-d H:i:s',time());
		if(M($this->dbName)->add($data))$result=array('code'=>0,'msg'=>'添加成功');
		else $result=array('code'=>1,'msg'=>'添加失败');
		echo json_encode($result);
	}
	// 修改作品页面
	public function updatePage(){
		tag('func2');
		$where['std_id']=$_ENV['user']['id'];
		$where['product_id']=I('get.id');
		$data['production']=M($this->dbName)->where($where)->find();
		$this->assign('data',$data);
		// dump($data);
		R('Page/productionUpdate');
	}
	// 修改作品接口
	public function update(){
		tag('func2');
		$data=I('post.');
		
		if($data['file']){
			$data['product_file']=$data['file'];
			$data['product_type']=2;
		}
		// 图片上传
		$img=R('Myfunc/Myfunc/upload');
		if($img){
			$data['product_file']=$img;
			$temp=explode('.',$data['product_file']);
			if($temp[1]=='jpeg'||$temp[1]=='jpg'||$temp[1]=='png'||$temp[1]=='gif')$data['product_type']=0;
			else $data['product_type']=1;
			
            $oldFile=M($this->dbName)->where($where)->field('product_file')->find();
            $oldFile='.'.$oldImg['product_file'];
		}

		if(!$data)return;
		$where['product_id']=I('post.product_id');
		$data['update_time']=date('Y-m-d H:i:s',time());
		if(M($this->dbName)->where($where)->save($data)!==false){
			$result=array('code'=>0,'msg'=>'修改成功');
			// 删除原来图片
            if(file_exists($oldFile))unlink($oldFile);
		}
		else $result=array('code'=>1,'msg'=>'修改失败');
		echo json_encode($result);
	}
	// 删除作品
	public function del(){
		tag('func2');
		$where['product_id']=I('post.product_id');

        $oldFile=M($this->dbName)->where($where)->field('product_file')->find();
        $oldFile='.'.$oldImg['product_file'];
        
		if(M($this->dbName)->where($where)->delete()){
			$result=array('code'=>0,'msg'=>'删除成功');
			// 删除原来图片
            if(file_exists($oldFile))unlink($oldFile);
		}
		else $result=array('code'=>1,'msg'=>'删除失败');
		echo json_encode($result);
	}
}